﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Talon
{
    class ProjectInfo
    {
        public string name { get; set; }
        public string path { get; set; }
    }
    class WorkspaceConfig
    {
        public string currentProject { get; set; }
        public List<ProjectInfo> recentProjects { get; set; }
    }
    class TalonsWorkSpace
    {
        public string CurrentProjectPath { get; set; }
        public string CurrentProjectDir { get; set; }
        public WorkspaceConfig Config { get; set; }
        public static string WorkspaceConfigFile { get; set; }
        public event EventHandler ReloadMenuEvent;//更新菜单事件
        public TalonsWorkSpace(string workspacePath)
        {
            WorkspaceConfigFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), string.Format("{0}.json",workspacePath));
        }
        
        /// <summary>
        /// 加载工作空间json文件
        /// </summary>
        /// <returns></returns>
        public string Load()
        {
            // 判断文件是否存在
            if (!File.Exists(WorkspaceConfigFile))
            {
                // 创建文件并写入一个空的JSON对象
                using (StreamWriter sw = File.CreateText(WorkspaceConfigFile))
                {
                    sw.Write("{}");
                }
                return null;
            }
            // 读取config.json文件
            string jsonString = File.ReadAllText(WorkspaceConfigFile);
            try
            {
                // 解析 JSON
                Config = JsonConvert.DeserializeObject<WorkspaceConfig>(jsonString);
                if (Config.currentProject == null) return null;

                foreach (var item in Config.recentProjects)
                {
                    if(item.name == Config.currentProject)
                    {
                        CurrentProjectPath = item.path;
                    }
                }
                ReloadMenuEvent(this, EventArgs.Empty);
                return CurrentProjectPath;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 清除最近项目
        /// </summary>
        public void ClearRecentProject()
        {
            Config.recentProjects.Clear();
            SetCurrentProject(Config.currentProject, CurrentProjectPath);//清空后此处会刷新最近项目
            // 将MyJsonObject对象序列化为JSON字符串
            string json = JsonConvert.SerializeObject(Config, Formatting.Indented);
            File.WriteAllText(WorkspaceConfigFile, json);
        }
        /// <summary>
        /// 修改当前项目
        /// </summary>
        /// <param name="projectName"></param>
        /// <param name="projectPath"></param>
        public void SetCurrentProject(string projectName, string projectPath)
        {
            Config.currentProject = projectName;
            CurrentProjectPath = projectPath;
            CurrentProjectDir = Path.GetDirectoryName(projectPath);
            if (!IsProjcetIsRecent(projectName))
            {
                Config.recentProjects.Add(new ProjectInfo { name = projectName, path = projectPath });
                ReloadMenuEvent(this, EventArgs.Empty);//当打开的项目不是最近项目时才会刷新以新增
            }
            else
            {
                EditProjectPath(projectName, projectPath);
            }

            // 将MyJsonObject对象序列化为JSON字符串
            string json = JsonConvert.SerializeObject(Config, Formatting.Indented);
            File.WriteAllText(WorkspaceConfigFile, json);
        }

        void EditProjectPath(string projectName, string projectPath)
        {
            if (Config.recentProjects == null)
            {
                Config.recentProjects = new List<ProjectInfo>();
            }
            foreach (var item in Config.recentProjects)
            {
                if (item.name == projectName)
                {
                    item.path = projectPath;
                }
            }
        }

        bool IsProjcetIsRecent(string projectName)
        {
            if(Config.recentProjects == null)
            {
                Config.recentProjects = new List<ProjectInfo>();
            }
            foreach (var item in Config.recentProjects)
            {
                if (item.name == projectName)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
